package com.tiger.cloud.common.redis.queue.test;

import java.sql.Timestamp;

import com.tiger.cloud.common.redis.RedisTemplate;
import com.tiger.cloud.common.redis.queue.MessageConsumer;
import com.tiger.cloud.common.redis.queue.MessageListener;
import com.tiger.cloud.common.redis.queue.RedisQueue;
import com.tiger.cloud.common.redis.queue.TaskMonitor;
import com.tiger.cloud.util.LoopRunnable;

import redis.clients.jedis.JedisPool;

/**
 * Hello world!
 *
 */
public class App {

	public static void main(String[] args) {
		JedisPool jedisPool = new JedisPool("127.0.0.1", 6379);
		RedisTemplate redisTemplate = new RedisTemplate(jedisPool);
		RedisQueue redisQueue = new RedisQueue(redisTemplate, "queue");

		TaskMonitor taskMonitor = new TaskMonitor(redisQueue);
		taskMonitor.start();

		MessageConsumer messageConsumer = new MessageConsumer(redisQueue, new MessageListener() {

			@Override
			public void onMessage(String message) {
				System.out.println(message);
			}
		});
		messageConsumer.startAsync();
		LoopRunnable lr = new LoopRunnable(new Runnable() {
			public void run() {
				for (int i = 0; i < 1000; i++) {
					redisQueue.sendMessage("message[" + new Timestamp(System.currentTimeMillis()) + "]");
				}
			}
		});
		lr.setSleepInterval(100);
		Thread t = new Thread(lr);
		t.start();
	}
}
